package org.jumiz.dbview.own.controller;

import org.jumiz.dbview.config.R;
import org.jumiz.dbview.own.entity.ConnectEntity;
import org.jumiz.dbview.own.service.ConnectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/own")
public class ConnectController {

    @Autowired
    private ConnectService connectService;

    @GetMapping("/list")
    public Mono<R.Response> findAll() {
        return connectService.getAll().reduce(new ArrayList<ConnectEntity>(), (connectEntities, entity) -> {
            connectEntities.add(entity);
            return connectEntities;
        }).map(item -> R.success(item));
    }

    @PostMapping("/create")
    public Mono<R.Response> save(@RequestBody ConnectEntity entity) {
        return connectService.save(entity).map(item -> R.success(item));
    }

    @PostMapping("/testconnect")
    public Mono<R.Response> testConnect(@RequestBody ConnectEntity entity) {
        return connectService.testConnect(entity).map(item -> R.success(item));
    }

    @GetMapping("/expand")
    public Mono<R.Response> expand(Long id) {
        return connectService.getMenusData(id).map(item -> R.success(item));
    }

}
